<template>
  <view class="want-page">
    <view class="want-title">请填写您的需求信息，我们将为您对接卖家</view>
    <view class="want-form">
      <view class="form-list">
        <view class="list-title">真实姓名 <view style="color: #F52020;">*</view>
        </view>
        <view class="list-int">
          <up-input placeholder="请填写真实姓名" border="none" v-model="realName"></up-input>
        </view>
      </view>
      <view class="form-list">
        <view class="list-title">手机号码 <view style="color: #F52020;">*</view>
        </view>
        <view class="list-int">
          <up-input placeholder="请填写真实手机号码" type="number" maxlength="11" border="none" v-model="mobile"></up-input>
        </view>
      </view>
      <view class="form-list">
        <view class="list-title">微信号</view>
        <view class="list-int">
          <up-input placeholder="请填写您的微信号" border="none" v-model="wechat"></up-input>
        </view>
      </view>
      <view class="form-list">
        <view class="list-title">需求描述</view>
        <view class="list-int">
          <up-input placeholder="请简单描述您的购买需求" border="none" v-model="desc"></up-input>
        </view>
      </view>
    </view>
    <view class="want-btn">
      <up-button text="提交 " shape="circle" color="#A4682F" @click="onsubmit"></up-button>
    </view>
  </view>
  <up-modal :show="modalShow" title="提交成功" contentTextAlign="center" confirmColor="#A4682F" content='提交成功,我们会尽快联系你'
    @confirm="onconfirm"></up-modal>
</template>

<script setup>
  import {
    intention
  } from './api.js'
  import {
    ref
  } from 'vue'
  import {
    onLoad
  } from '@dcloudio/uni-app'
  const taskId = ref()
  const realName = ref('')
  const mobile = ref('')
  const wechat = ref('')
  const desc = ref('')
  const modalShow = ref(false)

  onLoad((e) => {
    if (e && e.id) {
      taskId.value = e.id
    }
  })
  const onsubmit = (async () => {
    if(!realName.value){
      uni.showToast({
        title:"请填写真实姓名",
        icon:"none"
      })
      return
    }
    if(!mobile.value){
      uni.showToast({
        title:"请填写真实手机号",
        icon:"none"
      })
      return
    }
    const regex = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    if (!regex.test(mobile.value)) {
      uni.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
      return
    }
    const res = await intention({
      task_id: taskId.value,
      real_name: realName.value,
      mobile: mobile.value,
      wechat: wechat.value,
      desc: desc.value
    })
    if (res && res.code === 1) {
      modalShow.value = true
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none'
      })
    }
  })

  function onconfirm() {
    uni.switchTab({
      url: "/pages/index/index"
    })
  }
</script>

<style lang="scss" scoped>
  .want-page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    padding: 30rpx;

    .want-title {
      font-size: 28rpx;
      font-weight: 400;
      color: rgba(153, 153, 153, 1);

    }

    .want-form {
      width: 100%;
      display: flex;
      flex-direction: column;
      border-radius: 16rpx;
      background: #FFFFFF;
      box-sizing: border-box;
      padding: 0 30rpx;
      margin-top: 30rpx;

      .form-list {
        width: 100%;
        height: 88rpx;
        display: flex;
        align-items: center;
        border-bottom: 2rpx solid #ebebeb;

        .list-title {
          width: 25%;
          font-size: 28rpx;
          font-weight: 400;
          color: rgba(51, 51, 51, 1);
          display: flex;
        }

        .list-int {
          flex: 1;
        }
      }

      .form-list:last-child {
        border: none;
      }
    }

    .want-btn {
      width: 690rpx;
      height: 88rpx;
      border-radius: 44rpx;
      background: $color;
      display: flex;
      align-items: center;
      margin-top: 48rpx;
    }
  }
</style>